<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MActionFilter extends MApplicationComponent implements MIController
{
    public $action = NULL;
    
    public function invoke ($uri = null)
    {
                                switch ($this->action) {
            case "files":
                if (@$_SERVER["REQUEST_METHOD"] === "GET") {
                    return;
                } else {
                    $this->action = "files_post";
                }
                break;
            case "fileops":
                $this->action = $this->handleGetAction($uri);
                if ($this->action == "delete") {
                    return;
                }
                break;
            case "files_sec":
            case "restore":
            case "files_put":
                break;
            default:                 return;
        }
        
                $user = MUserManager::getInstance()->getCurrentUser();
        $user_id = $user->user_id;
        $space = self::handleGetUserSpace($user_id);
        $usedSpace = self::handleGetUsedSapce($user_id);
                                switch ($this->action) {
            case "files_put":
            case "filse_post":
                $usedSpace += $_SERVER["CONTENT_LENGTH"];
                break;
            default:
                ;
                break;
        }
                                if ($usedSpace >= $space) {
            throw new MFileopsException(FXP::t("User is over storage quota."), 
            MConst::HTTP_CODE_507);
        }
    }
    
    public static function handleGetUserSpace ($user_id)
    {
        $space = DEFAULT_USER_SPACE;
        $metas = new MUserMetas();
        $user_meta = $metas->queryMetaByKey($user_id, "space");
        if ($user_meta) {
            $space = $user_meta[0]["meta_value"];
        } else {
                        $option = new MOptions();
            $row = $option->queryMeta("site_default_space");
            if ($row) {
                $space = $row->option_value;
            }
        }
        $space = $space * 1024 * 1024;         return $space;
    }
    
    public static function handleGetUsedSapce ($user_id)
    {
                                $file = new MFiles();
        $used = $file->getUsedSpace($user_id);
        $usedSpace = 0;
        if (count($used) > 0) {
            $usedSpace = $used[0]["usedSpace"];
        }
        return $usedSpace;
    }
    
    private function handleGetAction ($uri)
    {
        $fileops_controller = null;
        $parts = array_slice(explode('/', $uri), 2);
        if (count($parts) < 1) {
            throw new MFileopsException(
            FXP::t('{class} do not call an action', 
            array('{class}' => get_class($this))));
        }
        $parts = $parts[0];
        if ($pos = strpos($parts, '?')) {
            $parts = substr($parts, 0, $pos);
        }
        return $parts;
    }
}